package data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashSet;

import org.apache.lucene.search.ScoreDoc;

import lucene.PaperSearcher;

public class AuthorDoc {

	private PaperSearcher searcher;
	private HashSet<String> authors = new HashSet<String>();

	public AuthorDoc() throws Exception {
		File indexDir = new File("D:\\CityU\\project\\bibsonomy\\"
				+ "\\BibIndex\\");
		this.searcher = new PaperSearcher(indexDir);
		initAuthors();
	}

	private void initAuthors() throws Exception {
		String path = "D:\\CityU\\project\\bibsonomy\\data" + "\\authorMap.txt";
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line = null;
		while ((line = reader.readLine()) != null) {
			line = line.trim();
			// System.out.println(line);
			String[] tokens = line.split("\t");
			// String author = tokens[0];
			String id = tokens[1];
			authors.add(id);
		}
	}

	public void statAuthorDoc() throws Exception {
		String path = "D:\\CityU\\project\\bibsonomy\\data"
				+ "\\authorDocNum.txt";
		BufferedWriter writer = new BufferedWriter(new FileWriter(path));
		try {
			for (String author : authors) {
				ScoreDoc[] docs = searcher.retrieveResultsByAuthorId(author);
				System.out.println(author + "\t" + docs.length);
				writer.write(author + "\t" + docs.length);
				writer.newLine();
			}
		} finally {
			writer.flush();
			writer.close();
		}
	}

	public static void main(String[] args) throws Exception{
		AuthorDoc app = new AuthorDoc();
		app.statAuthorDoc();
		
	}
	
}
